package com.game.collision;

import java.util.LinkedList;
import java.util.Random;

import com.game.bullet.Bullet;
import com.game.character.Character;
import com.game.character.TypeCharacter.TypeObject;
import com.game.tiledmap.Tiled;
import com.game.tiledmap.TiledMap;
import com.game_ltdd.Resrouce;

public class CollisionCheck {
	public static void collisionOnMap(Character character, TiledMap map) {
		LinkedList<Tiled> colLinkedList = new LinkedList<Tiled>();
		colLinkedList = map.getListObjectMapToCheckCollision(character
				.getBoundBox());
		for (int index = 0; index < colLinkedList.size(); index++) {
			if (character.type == TypeObject.Bullet) {
				if (colLinkedList.get(index).canCollision) {
					character.isExplore = true;
					//Resrouce.bulletexplosion.play(1);
					if (colLinkedList.get(index).canBeDestroy) {
						map.tiledLayer.get(colLinkedList.get(index).posOnMap).isDestroy = true;
						int i = colLinkedList.get(index).posOnMap
								/ map.mapHeight;
						int j = colLinkedList.get(index).posOnMap
								% map.mapWidth;
						map.map[i][j] = 0;
					}
					break;
				}
			}
			if (character.type == TypeObject.Player
					|| character.type == TypeObject.Enemy) {
				if (colLinkedList.get(index).canCollision) {
					character.position = character.prevPosition;
					character.isChangeDir = true;
				}
			}
		}
	}

	public static void collisionWithBullet(Character character,
			LinkedList<Bullet> listBullet) {
		Random random = new Random();
		for (Bullet bullet : listBullet) {
			if (bullet.isExplore || bullet.isDeath) {
				continue;
			}
			if (bullet.user.type == TypeObject.Enemy) {
				if (character.type == TypeObject.Player && character.getBoundBox().intersect(bullet.getBoundBox())) {
					character.dameReceive = bullet.minimumDamge + random.nextInt(bullet.rangeDamge);
					bullet.isExplore = true;
					Resrouce.bulletexplosion.play(1, 0);
					continue;
				}
			}
			if (bullet.user.type == TypeObject.Player) {
				if (character.type == TypeObject.Enemy && character.getBoundBox().intersect(bullet.getBoundBox())) {
					character.dameReceive = bullet.minimumDamge + random.nextInt(bullet.rangeDamge);
					bullet.isExplore = true;
					Resrouce.bulletexplosion.play(1, 0);
				}
			}
		}
	}
}
